/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.publish.service;

import com.thinkgem.jeesite.common.mapper.JaxbMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipment;
import com.thinkgem.jeesite.modules.publish.dao.EleTempletPublishDao;
import com.thinkgem.jeesite.modules.publish.entity.EleTempletPublish;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.templet.entity.EleTemplet;
import com.thinkgem.jeesite.modules.templet.entity.EleTempletSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资源发布包Service
 * @author unnamed
 * @version 2017-06-04
 */
@Service
@Transactional(readOnly = true)
public class EleTempletPublishService extends CrudService<EleTempletPublishDao, EleTempletPublish> {

	public EleTempletPublish get(String id) {
		return super.get(id);
	}
	
	public List<EleTempletPublish> findList(EleTempletPublish eleTempletPublish) {
		return super.findList(eleTempletPublish);
	}
	public List<EleTempletPublish> findListByEquipmentId(String equipmentId,String status) {
		return dao.findListByEquipmentId(equipmentId, status);
	}
	public List<EleTempletPublish> findListByEquipments(List<EleEquipment> list,String status) {
		return dao.findListByEquipments(list, status);
	}
	public Page<EleTempletPublish> findPublishPage(Page<EleTempletPublish> eleTempletPage, EleTempletPublish eleTemplet){
		eleTemplet.setPage(eleTempletPage);
		eleTempletPage.setList(dao.findPublishList(eleTemplet));
		return eleTempletPage;
	}
	public Page<EleTempletPublish> findPublishPageForCheck(Page<EleTempletPublish> eleTempletPage, List<User> users,String status){
		eleTempletPage.setList(dao.findEleTempletForCheck(users, status));
		return eleTempletPage;
	}
	@Transactional(readOnly = false)
	public void save(EleTempletPublish eleTempletPublish) {
		super.save(eleTempletPublish);
	}
	@Transactional(readOnly = false)
	public void updatePublishStatus(String publishId,String status) {
		dao.updatePublishStatus(publishId, status);
	}
	
	@Transactional(readOnly = false)
	public void delete(EleTempletPublish eleTempletPublish) {
		super.delete(eleTempletPublish);
	}

    @Transactional(readOnly = false)
    public void batchInsertTemplet(List<EleTempletPublish> publishList){
        for(EleTempletPublish entity : publishList){
            if(entity.getIsNewRecord()){
                entity.preInsert();
            }
        }
        Map<String,List<EleTempletPublish>> params = new HashMap<String,List<EleTempletPublish>>();
        params.put("list", publishList);
        dao.batchInsertTemplet(params);
    }

	@Transactional(readOnly = false)
	public Result delApprovalPublish(EleTempletPublish publish){
		int result = dao.delApprovalPublish(publish);
		if(result>0){
			return new Result(Result.SUCCESS, "删除审核记录操作成功!", null);
		}else{
			return new Result(Result.FAILURE, "删除审核记录完成!", null);
		}
	}

	@Transactional(readOnly = false)
	public Result checkPublish(EleTempletPublish publish){
		int result = dao.check(publish);
		if(result>0){
			return new Result(Result.SUCCESS, "审核操作成功!", null);
		}else{
			return new Result(Result.FAILURE, "审核操作完成!", null);
		}
	}

	@Transactional(readOnly = false)
	public int delApplyTempletStatus(String templets, String status){
		return dao.delApplyTempletStatus(templets, status);
	}

	@Transactional(readOnly = false)
	public int delApprovalTempletStatus(String templets, String status){
		return dao.delApprovalTempletStatus(templets, status);
	}
	@Transactional(readOnly = false)
	public int bathcUpdateTempletStatus(String templets, String status, String checker){
		return dao.bathcUpdateTempletStatus(templets, status, checker);
	}

	public String eleTempletToXml(EleTemplet templet){
		if(null != templet){
			double zoom_width = 1;//templet.getWidth() / EleTemplet.LAND_WIDTH;
			double zoom_height = 1;//templet.getHeight() / EleTemplet.LAND_HEIGHT;
			if("port".equals(templet.getDisplaymode())){
				zoom_height = templet.getHeight();// EleTemplet.PORT_HEIGHT;
			}

			List<EleTempletSource> templetSourceList = templet.getEleTempletSourceList();

			if(null != templetSourceList){

				for(int i = templetSourceList.size() - 1; i >= 0; i--){
					EleTempletSource templetSource = templetSourceList.get(i);
					if(!EleTempletSource.DEL_FLAG_NORMAL.equals(templetSource.getDelFlag())){
						templetSourceList.remove(templetSource);
					}
					else{
						templetSource.setCfgfile(templetSource.getSourceType() + ".xml");
						templetSource.setRespath(templetSource.getSourceType());

						String Height = String.valueOf(Math.round(Integer.parseInt(templetSource.getHeight()) * zoom_width));
						String Xloc = String.valueOf(Math.round(Integer.parseInt(templetSource.getXloc()) * zoom_width));
						templetSource.setHeight(Height);
						templetSource.setXloc(Xloc);
						String Width = String.valueOf(Math.round(Integer.parseInt(templetSource.getWidth()) * zoom_height));
						String Yloc = String.valueOf(Math.round(Integer.parseInt(templetSource.getYloc()) * zoom_height));
						templetSource.setWidth(Width);
						templetSource.setYloc(Yloc);
					}
				}
			}
			return JaxbMapper.toXml(templet, "utf-8");
		}
		else{
			return "";
		}
	}
   
}